package com.dgshanger.sanhxiaofeisc.ui.recharge

import android.annotation.SuppressLint
import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AnimationUtils
import com.dgshanger.sanhxiaofeisc.MainActivity
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.VipDetailEntity
import com.dgshanger.sanhxiaofeisc.entity.VipEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.RechargeContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.RechargePresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.person.purse.MembershipCardActivity
import com.dgshanger.sanhxiaofeisc.ui.person.purse.PurseActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecFragment
import kotlinx.android.synthetic.main.fragment_recharge.*
import kotlinx.android.synthetic.main.item_rechare_vip.view.*
import kotlinx.android.synthetic.main.view_nearest_vip_rcd.view.*


/**
 * RechargeFragment
 * 沉迷学习不能自拔
 * Describe：充值页面
 * Created by 雷小星🍀 on 2018/4/10 18:09.
 */
class RechargeFragment : BaseMvpLcecFragment<SwipeRefreshLayout, VipEntity, RechargeContract.Model, RechargeContract.View, RechargePresenter>(), RechargeContract.View {

    override fun setRefresh(b: Boolean) {
        contentView.isRefreshing = b
    }

    override fun createPresenter(): RechargePresenter = RechargePresenter()

    override fun loadData(refresh: Boolean) {
        presenter.loadRechargeInfo()
    }

    override fun setData(data: VipEntity?) {
        showContent()

        if (Constants.isLogin()) {
            ImageLoader.loadAvatar(mContext, ib_avatar, Constants.getUserEntity()?.avatar)
        } else {
            ib_avatar.setImageResource(R.color.colorDivler)
        }

        view_vip_content.removeAllViews()
        for (entity in data!!.vip_list.iterator()) {
            val createVipView = createVipView(entity)
            view_vip_content.addView(createVipView)
            createVipView.animation = AnimationUtils.makeInChildBottomAnimation(mContext)
        }

        view_nearest_vip_rcd_content.removeAllViews()
        for (entity in data.user_nearest_vip_rcd.iterator()) {
            val vipRcdView = createNearestVipRcdView(entity)
            view_nearest_vip_rcd_content.addView(vipRcdView)
            vipRcdView.animation = AnimationUtils.makeInChildBottomAnimation(mContext)
        }
    }

    override fun getFragmentLayoutId(): Int = R.layout.fragment_recharge

    override fun initView(view: View?) {
        super.initView(view)
        contentView.setColorSchemeResources(R.color.colorAccent)
    }

    @SuppressLint("SetTextI18n")
    private fun createVipView(entity: VipDetailEntity): View {
        val vipView = LayoutInflater.from(mContext).inflate(R.layout.item_rechare_vip, null)
        vipView.tv_recharge_money.text = entity.recharge_money
        vipView.tv_level_name.text = entity.level_name
        vipView.tv_cycle.text = " " + entity.return_time + "期"
        vipView.tv_dscp.text = entity.dscp
        vipView.tv_return.text = "${entity.recharge_money + getString(R.string.wallet_money)} +${entity.return_integral_num + getString(R.string.integral)}"
        vipView.setOnClickListener {
            val intent = Intent(mContext, VipCardActivity::class.java)
            intent.putExtra(Constants.KEY_INTENT_VIP, entity.id)
            startActivity(intent)
        }
        return vipView
    }


    @SuppressLint("SetTextI18n")
    /**
     * 创建最近记录
     */
    private fun createNearestVipRcdView(entity: VipEntity.UserNearestVipRcdBean): View {
        val vipRcdView = LayoutInflater.from(mContext).inflate(R.layout.view_nearest_vip_rcd, null)
        vipRcdView.tv_nearest_recharge_money.text = "¥${entity.recharge_money}"
        vipRcdView.tv_present.text = "${getString(R.string.wallet_money)}: ${entity.present_wallet_money} ${getString(R.string.integral)}:${entity.present_integral} "
        vipRcdView.tv_return_time.text = "${entity.return_time}期"
        return vipRcdView
    }

    override fun initEvent(view: View?) {

        ib_avatar.setOnClickListener {
            //个人中心
            val intent = Intent(mContext, MainActivity::class.java)
            intent.putExtra(Constants.KEY_INTENT_MAIN, MainActivity.INDEX_PERSON)
            startActivity(intent)
        }

        tv_vip_purse.setOnClickListener {
            //会员钱夹
            startActivity(MembershipCardActivity::class.java)
        }
        tv_my_purse.setOnClickListener {
            //我的钱包
            startActivity(PurseActivity::class.java)
        }
        contentView.setOnRefreshListener {
            if (!presenter.isLoading!!) {
                loadData(true)
            }
        }
    }

    override fun initData() {
        loadData(true)
    }
}